package zheezes.eisp.collect.model;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.apache.log4j.Logger;

import zheezes.eisp.data.orm.CalcTask;
import zheezes.eisp.data.orm.CollectTask;
import zheezes.eisp.data.orm.Concentrator;
import zheezes.eisp.data.orm.ResMeter;
import zheezes.util.ArrayUtil;
import zheezes.util.DateFormatUtil;


public abstract class ConResMeterDailyFrozenCollectTaskHandler extends CollectTaskHandler {
	private static Logger logger = 
		Logger.getLogger(ConResMeterDailyFrozenCollectTaskHandler.class);

	private List<ResMeter> list = null;
	protected int batch = 10;

	@SuppressWarnings({ "unchecked", "rawtypes" })
	protected List<ResMeter> load() {
		if(list != null) {
			list.clear();
		}
		list = dataAccessor.queryConNoDailyFrozenResMeter(task.getConId(), task.getDataDate());		
		if (list != null) {
			logger.debug(String.format(
					"total %d meters not collected int con[%d] at %s", 
					list.size(), task.getConId(), DateFormatUtil.getDateFmt().format(task.getDataDate())));
			return (List) ((ArrayList) list).clone();
		}
		return null;
	}

	protected ResMeter findBySeq(int seq) {
		if(list != null) {
			for (ResMeter meter : list) {
				// must change to primitive type or use the equal
				if (meter.getSeq().intValue() == seq) {
					return meter;
				}
			}
		}
		return null;
	}

	protected ResMeter findByNo(byte[] no) {
		if(list != null) {
			String sno = ArrayUtil.sprintf(no, "%02X");
			for (ResMeter meter : list) {
				if (sno.equals(meter.getNo())) {
					return meter;
				}
			}
		}
		return null;
	}
	
	protected void refreshTaskStat() {
		task.setTotalNum(dataAccessor.countResMeterByCon(task.getConId()));
		task.setCollectedNum(dataAccessor.countResMeterWithDailyFrznByCon(task.getConId(), task.getDataDate()));
		task.setStatus(task.getTotalNum().equals(task.getCollectedNum()) ? CollectTask.STATUS_FINISH : CollectTask.STATUS_QUEUE);
		/* you should not to update the task last_run_date, because
		 * if the program is shutdown by user, you should restart all the task which now is running
		 * so, if the task is terminated by user, not to update last_run_date
		 */
		// task.setLastRunDate(Calendar.getInstance().getTime());
		dataAccessor.updateCollectTask(task);
	}
	
	/**
	 * check if need to add daily calc
	 */
	protected void checkConResMeterDailyUse(Concentrator con, Calendar cal) {
		logger.debug(String.format("check con[%s] %s daily use", con.getLogicAddr(), DateFormatUtil.getDateFmt().format(cal.getTime())));
		CalcTask calcTask = new CalcTask();
		calcTask.setTaskType(CalcTask.TASK_CALC_USE);
		calcTask.setTargetType(CalcTask.TARGET_CONCENTRATOR);
		calcTask.setTargetId(con.getId());
		calcTask.setStatus(CalcTask.STATUS_QUEUE);
		calcTask.setSource(CalcTask.SOURCE_AUTO);

		Calendar tc = Calendar.getInstance();

		// yestday daily use
		tc.setTime(cal.getTime());
		tc.add(Calendar.DATE, -1);
		calcTask.setDate(tc.getTime());
		calcTask.setCreateDate(Calendar.getInstance().getTime());
		dataAccessor.replaceCalcTask(calcTask);

		// today daily use
		tc.setTime(cal.getTime());
		calcTask.setDate(tc.getTime());
		calcTask.setCreateDate(Calendar.getInstance().getTime());
		dataAccessor.replaceCalcTask(calcTask);
	}
}
